#include "qenterpin.h"
#include <QtGui/QMessageBox>
#include <QTimer>
#include "qenternumpaycard.h"

QEnterPIN::QEnterPIN(QWidget *parent)
    : QWidget(parent),PINcode( "" ),type_( QEnterPIN::PIN )
{
	ui.setupUi(this);
    // setWindowModality( Qt::WindowModal );
	setWindowFlags(Qt::SplashScreen);
    ui.lePINcode->setEchoMode( QLineEdit::Password );
    ui.lePINcode->clear();
    ui.frameEnterPIN->setObjectName( QString::fromUtf8( "frameEnterPIN" ) );
}

QEnterPIN::~QEnterPIN()
{

}
void QEnterPIN::paintEvent( QPaintEvent * event ){

	QPainter painter( this );
	QBitmap bitmap;
	if( isPIN() )
		bitmap = QBitmap(".//img/wndEnterPIN.png" );
	if( isPUK() )
		bitmap = QBitmap(".//img/wndEnterPUK.PNG" );

	QRegion region( bitmap);
	QRect boundRect = region.boundingRect();

	if ( isPIN() )
		ui.frameEnterPIN->setStyleSheet( QString::fromUtf8( " #frameEnterPIN{	background-image: url(:/mainWnd/img/wndEnterPIN.png) }" ) );
	if ( isPUK() )
		ui.frameEnterPIN->setStyleSheet( QString::fromUtf8( " #frameEnterPIN{	background-image: url(:/mainWnd/img/wndEnterPUK.PNG) }" ) );

	setMask( region.boundingRect());
}
QLineEdit * QEnterPIN::getLEPinCode(){
	return ui.lePINcode;
}
void QEnterPIN::OnOkEnterPIN(){

	if( isPIN() ){
		if( ui.lePINcode->text() != "" ){
			PINcode = ui.lePINcode->text();

			QTimer *timer = new QTimer(this);
			connect(timer, SIGNAL(timeout()), this, SLOT( update() ));
			timer->start(3000); //wait for 3 secs to close the form

			QEnterNumPayCard * wnd = new QEnterNumPayCard;
			balanceState_ = wnd->balanceState;
			balanceState_->show();

			//QMessageBox::information( this,tr("Состояние баланса"),tr("Баланс Вашего счета составляет 133 рубля"),1);
			close();
			return;
		}
		QMessageBox::information( this,tr("Информация"),tr("PIN не введен!"),1);
	}

	//TODO what if pin code will not be entered???
}
void QEnterPIN::update(){

	balanceState_->close();

}
QString& QEnterPIN::getPINcode(){
	return PINcode;
}
